package com.yanqu.road.server.manger.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorCellData;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.HuaShiGangActivityBusiness;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 除花石纲外沙漠宝库的配置
 *
 * @Author wpj
 * @Date 2020/12/13
 */
public class DigMgr {
    public static Map<Integer, Map<Integer, HuaShiGangFloorConfig>> floorConfigMap = new ConcurrentHashMap<>();

    public static void loadActivityConfig(int activityId) {
        Map<Integer, HuaShiGangFloorConfig> map = StrongholdActivityMgr.getDesertConfigMap();
        floorConfigMap.put(activityId, map);
    }

    public static void loadActivityCommonConfig(int activityId) {
        Map<Integer, HuaShiGangFloorConfig> map = null;
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(activityInfo.getType() == eActivityType.PeakStrongHold.getValue()){
            map = PeakStrongholdActivityMgr.getConfig().getDesertConfigMap();
        }
        if(map != null){
            floorConfigMap.put(activityId, map);
        }
    }

    public static Map<Integer, Map<Integer, HuaShiGangFloorConfig>> getFloorConfigMap() {
        return floorConfigMap;
    }

    public static Map<Integer, HuaShiGangFloorConfig> getFloorConfigMap(int activityId) {
        return floorConfigMap.get(activityId);
    }

    public static HuaShiGangFloorConfig getFloorConfigMap(int activityId, int floor) {
        return floorConfigMap.get(activityId).get(floor);
    }

    public static List<HuaShiGangFloorCellData> buildCellDataList(HuaShiGangFloorConfig config) {

        List<HuaShiGangFloorCellData> list = new ArrayList<>();
        HuaShiGangFloorCellData special = new HuaShiGangFloorCellData();
        special.setOpen(false);
        special.setReward(StringUtils.stringToStringList(config.getSpecialReward(), "\\|").get(0));
        special.setSpecial(true);
        list.add(special);

        List<String> normalRewardList = new ArrayList<>();
        List<Integer> weightList = new ArrayList<>();
        List<String> rewardParamList = StringUtils.stringToStringList(config.getRewards(), "\\|");
        for (String param : rewardParamList) {
            List<String> p = StringUtils.stringToStringList(param, ",");
            String reward = p.get(0);
            int weight = Integer.valueOf(p.get(1));
            normalRewardList.add(reward);
            weightList.add(weight);
        }

        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            HuaShiGangFloorCellData normal = new HuaShiGangFloorCellData();
            normal.setOpen(false);
            normal.setSpecial(false);
            normal.setReward(normalRewardList.get(RandomHelper.getRandomIndexByWeight(weightList, random)));
            list.add(normal);
        }
        Collections.shuffle(list);
        return list;
    }

    public static ActivityInfo getActivityInfo(int activityId) {
        // 取据点战
        ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
        if (activityInfo != null && activityId == activityInfo.getActivityId()) {
            return activityInfo;
        }
        //巅峰据点战
        ActivityInfo activity102Info = PeakStrongholdActivityMgr.getActivityInfo();
        if (activity102Info != null && activityId == activity102Info.getActivityId()) {
            return activity102Info;
        }
        return null;
    }
}
